package com.wiloon.androidx.list;

import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wiloon.androidx.R;
import com.wiloon.androidx.BaseActivity;

import org.apache.log4j.Logger;

/**
 * Created by wangyue on 8/12/2014.
 */

public class ExpandableListViewActivity extends BaseActivity {
    Logger logger = Logger.getLogger(ExpandableListViewActivity.class);

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.expandable_list_view);
        final ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter() {
            int[] logos = new int[]{R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
            private String[] generalsTypes = new String[]{"A", "B", "C"};
            private String[][] generals = new String[][]{
                    {"A0", "A1", "A2"},
                    {"B0", "B1", "B2", "B3"},
                    {"C0", "C1"}
            };

            TextView getTextView() {
                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                TextView textView = new TextView(
                        ExpandableListViewActivity.this);
                textView.setLayoutParams(layoutParams);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setPadding(120, 0, 0, 0);
                textView.setTextSize(30);
                textView.setTextColor(Color.BLUE);
                return textView;
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public int getGroupCount() {
                return generalsTypes.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return generals[groupPosition].length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                return generalsTypes[groupPosition];
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return generals[groupPosition][childPosition];
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                LinearLayout linearLayout = new LinearLayout(
                        ExpandableListViewActivity.this);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);

                ImageView logo = new ImageView(ExpandableListViewActivity.this);
                logo.setImageResource(logos[groupPosition]);
                logo.setPadding(80, 0, 0, 0);
                linearLayout.addView(logo);
                TextView textView = getTextView();
                textView.setTextColor(Color.GRAY);
                textView.setText(getGroup(groupPosition).toString());
                linearLayout.addView(textView);
                return linearLayout;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                LinearLayout ll = new LinearLayout(
                        ExpandableListViewActivity.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);
                // ImageView generallogo = new ImageView(
                //         ExpandableListViewActivity.this);
                // generallogo
                //         .setImageResource(generallogos[groupPosition][childPosition]);
                // ll.addView(generallogo);
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition)
                        .toString());
                ll.addView(textView);
                return ll;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

            @Override
            public boolean areAllItemsEnabled() {
                return false;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public void onGroupExpanded(int groupPosition) {

            }

            @Override
            public void onGroupCollapsed(int groupPosition) {

            }

            @Override
            public long getCombinedChildId(long groupId, long childId) {
                return 0;
            }

            @Override
            public long getCombinedGroupId(long groupId) {
                return 0;
            }
        };

        ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        ExpandableListViewActivity.this,
                        expandableListAdapter.getChild(groupPosition, childPosition) + " Clicked",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}
